<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>A file handle - Boost.Outcome documentation</title>
<link rel="stylesheet" href="../../../css/boost.css" type="text/css">
<meta name="generator" content="Hugo 0.52 with Boostdoc theme">
<meta name="viewport" content="width=device-width,initial-scale=1.0"/>

<link rel="icon" href="../../../images/favicon.ico" type="image/ico"/>
<body><div class="spirit-nav">
<a accesskey="p" href="../../../tutorial/advanced/constructors/two-phase-init.html"><img src="../../../images/prev.png" alt="Prev"></a>
    <a accesskey="u" href="../../../tutorial/advanced/constructors.html"><img src="../../../images/up.png" alt="Up"></a>
    <a accesskey="h" href="../../../index.html"><img src="../../../images/home.png" alt="Home"></a><a accesskey="n" href="../../../tutorial/advanced/constructors/static-constructor.html"><img src="../../../images/next.png" alt="Next"></a></div><div id="content">
  <div class="titlepage"><div><div><h1 style="clear: both">A file handle</h1></div></div></div>
  <p>Borrowing from <a href="https://ned14.github.io/llfio/classllfio__v2__xxx_1_1file__handle.html"><code>llfio::file_handle</code></a>
which uses this design pattern<sup class="footnote-ref" id="fnref:1"><a href="#fn:1">1</a></sup>, here is a simplified <code>file_handle</code> implementation:</p>

<div class="code-snippet"><div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="k">class</span><span class="err"> </span><span class="nc">file_handle</span>
<span class="p">{</span>
  <span class="kt">int</span> <span class="n">_fd</span><span class="p">{</span><span class="o">-</span><span class="mi">1</span><span class="p">};</span>  <span class="c1">// file descriptor
</span><span class="c1"></span>  <span class="k">struct</span> <span class="n">stat</span> <span class="n">_stat</span>
  <span class="p">{</span>
    <span class="mi">0</span>
  <span class="p">};</span>  <span class="c1">// stat of the fd at open
</span><span class="c1"></span>
  <span class="c1">// Phase 1 private constexpr constructor
</span><span class="c1"></span>  <span class="k">constexpr</span> <span class="nf">file_handle</span><span class="p">()</span> <span class="p">{}</span>

<span class="k">public</span><span class="o">:</span>
  <span class="k">using</span> <span class="n">path_type</span> <span class="o">=</span> <span class="n">filesystem</span><span class="o">::</span><span class="n">path</span><span class="p">;</span>

  <span class="c1">//! The behaviour of the handle: does it read, read and write, or atomic append?
</span><span class="c1"></span>  <span class="k">enum</span> <span class="k">class</span><span class="err"> </span><span class="nc">mode</span> <span class="o">:</span> <span class="kt">unsigned</span> <span class="kt">char</span>  <span class="c1">// bit 0 set means writable
</span><span class="c1"></span>  <span class="p">{</span>
    <span class="n">unchanged</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span>
    <span class="n">none</span> <span class="o">=</span> <span class="mi">2</span><span class="p">,</span>        <span class="c1">//!&lt; No ability to read or write anything, but can synchronise (SYNCHRONIZE or 0)
</span><span class="c1"></span>    <span class="n">attr_read</span> <span class="o">=</span> <span class="mi">4</span><span class="p">,</span>   <span class="c1">//!&lt; Ability to read attributes (FILE_READ_ATTRIBUTES|SYNCHRONIZE or O_RDONLY)
</span><span class="c1"></span>    <span class="n">attr_write</span> <span class="o">=</span> <span class="mi">5</span><span class="p">,</span>  <span class="c1">//!&lt; Ability to read and write attributes (FILE_READ_ATTRIBUTES|FILE_WRITE_ATTRIBUTES|SYNCHRONIZE or O_RDONLY)
</span><span class="c1"></span>    <span class="n">read</span> <span class="o">=</span> <span class="mi">6</span><span class="p">,</span>        <span class="c1">//!&lt; Ability to read (READ_CONTROL|FILE_READ_DATA|FILE_READ_ATTRIBUTES|FILE_READ_EA|SYNCHRONISE or O_RDONLY)
</span><span class="c1"></span>    <span class="n">write</span> <span class="o">=</span> <span class="mi">7</span><span class="p">,</span>       <span class="c1">//!&lt; Ability to read and write (READ_CONTROL|FILE_READ_DATA|FILE_READ_ATTRIBUTES|FILE_READ_EA|FILE_WRITE_DATA|FILE_WRITE_ATTRIBUTES|FILE_WRITE_EA|FILE_APPEND_DATA|SYNCHRONISE or O_RDWR)
</span><span class="c1"></span>    <span class="n">append</span> <span class="o">=</span> <span class="mi">9</span>       <span class="c1">//!&lt; All mainstream OSs and CIFS guarantee this is atomic with respect to all other appenders (FILE_APPEND_DATA|SYNCHRONISE or O_APPEND)
</span><span class="c1"></span>  <span class="p">};</span>

  <span class="c1">// Moves but not copies permitted
</span><span class="c1"></span>  <span class="n">file_handle</span><span class="p">(</span><span class="k">const</span> <span class="n">file_handle</span> <span class="o">&amp;</span><span class="p">)</span> <span class="o">=</span> <span class="k">delete</span><span class="p">;</span>
  <span class="n">file_handle</span><span class="p">(</span><span class="n">file_handle</span> <span class="o">&amp;&amp;</span><span class="n">o</span><span class="p">)</span> <span class="k">noexcept</span> <span class="o">:</span> <span class="n">_fd</span><span class="p">(</span><span class="n">o</span><span class="p">.</span><span class="n">_fd</span><span class="p">)</span> <span class="p">{</span> <span class="n">o</span><span class="p">.</span><span class="n">_fd</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span> <span class="p">}</span>
  <span class="n">file_handle</span> <span class="o">&amp;</span><span class="k">operator</span><span class="o">=</span><span class="p">(</span><span class="k">const</span> <span class="n">file_handle</span> <span class="o">&amp;</span><span class="p">)</span> <span class="o">=</span> <span class="k">delete</span><span class="p">;</span>
  <span class="n">file_handle</span> <span class="o">&amp;</span><span class="k">operator</span><span class="o">=</span><span class="p">(</span><span class="n">file_handle</span> <span class="o">&amp;&amp;</span><span class="n">o</span><span class="p">)</span> <span class="k">noexcept</span>
  <span class="p">{</span>
    <span class="k">this</span><span class="o">-&gt;~</span><span class="n">file_handle</span><span class="p">();</span>
    <span class="k">new</span><span class="p">(</span><span class="k">this</span><span class="p">)</span> <span class="n">file_handle</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">move</span><span class="p">(</span><span class="n">o</span><span class="p">));</span>
    <span class="k">return</span> <span class="o">*</span><span class="k">this</span><span class="p">;</span>
  <span class="p">}</span>
  <span class="c1">// Destruction closes the handle
</span><span class="c1"></span>  <span class="o">~</span><span class="n">file_handle</span><span class="p">()</span>
  <span class="p">{</span>
    <span class="k">if</span><span class="p">(</span><span class="n">_fd</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span>
    <span class="p">{</span>
      <span class="k">if</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span> <span class="o">==</span> <span class="o">::</span><span class="n">close</span><span class="p">(</span><span class="n">_fd</span><span class="p">))</span>
      <span class="p">{</span>
        <span class="kt">int</span> <span class="n">e</span> <span class="o">=</span> <span class="n">errno</span><span class="p">;</span>
        <span class="n">std</span><span class="o">::</span><span class="n">cerr</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;FATAL: Closing the fd during destruction failed due to &#34;</span> <span class="o">&lt;&lt;</span> <span class="n">strerror</span><span class="p">(</span><span class="n">e</span><span class="p">)</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
        <span class="n">std</span><span class="o">::</span><span class="n">terminate</span><span class="p">();</span>
      <span class="p">}</span>
      <span class="n">_fd</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
    <span class="p">}</span>
  <span class="p">}</span>

  <span class="c1">// Phase 2 static member constructor function, which cannot throw
</span><span class="c1"></span>  <span class="k">static</span> <span class="kr">inline</span> <span class="n">outcome</span><span class="o">::</span><span class="n">result</span><span class="o">&lt;</span><span class="n">file_handle</span><span class="o">&gt;</span> <span class="n">file</span><span class="p">(</span><span class="n">path_type</span> <span class="n">path</span><span class="p">,</span> <span class="n">mode</span> <span class="n">mode</span> <span class="o">=</span> <span class="n">mode</span><span class="o">::</span><span class="n">read</span><span class="p">)</span> <span class="k">noexcept</span><span class="p">;</span>
<span class="p">};</span>
</code></pre></div><a href="https://github.com/boostorg/outcome/tree/master/doc/src/snippets/constructors.cpp#L58" class="code-snippet-url" target="_blank">View this code on Github</a></div>


<p>Note the default member initialisers, these are particularly convenient for
implementing phase 1 of construction. Note also the <code>constexpr</code> constructor,
which thanks to the default member initialisers is otherwise empty.</p>

<p>File handles are very expensive to copy as they involve a syscall to duplicate
the file descriptor, so we enable moves only.</p>

<p>The destructor closes the file descriptor if it is not -1, and if the close
fails, seeing as there is nothing else we can do without leaking the file
descriptor, we fatal exit the process.</p>

<p>Finally we declare the phase 2 constructor which is a static member function.</p>
<div class="footnotes">

<hr />

<ol>
<li id="fn:1">LLFIO uses Outcome &ldquo;in anger&rdquo;, both in Standard and Experimental configurations. If you would like a real world user of Outcome to study the source code of, it can be studied at <a href="https://github.com/ned14/llfio/">https://github.com/ned14/llfio/</a>.
 <a class="footnote-return" href="#fnref:1"><sup>[return]</sup></a></li>
</ol>
</div>


        </div><p><small>Last revised: February 08, 2019 at 22:18:08 UTC</small></p>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../../../tutorial/advanced/constructors/two-phase-init.html"><img src="../../../images/prev.png" alt="Prev"></a>
    <a accesskey="u" href="../../../tutorial/advanced/constructors.html"><img src="../../../images/up.png" alt="Up"></a>
    <a accesskey="h" href="../../../index.html"><img src="../../../images/home.png" alt="Home"></a><a accesskey="n" href="../../../tutorial/advanced/constructors/static-constructor.html"><img src="../../../images/next.png" alt="Next"></a></div></body>
</html>
